<template>
  <div>
    <el-card style="width: 40%;margin: 10px 0px 0px 250px ">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="form.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="form.nickname" ></el-input>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="form.age" ></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-input v-model="form.sex" ></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="form.address" ></el-input>
        </el-form-item>
        <el-form-item label="1:管理员 2:用户">
          <el-input v-model="form.role"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password" show-password ></el-input>
        </el-form-item>
      </el-form>
      <div style="text-align: center">
        <el-button type="primary" @click="update">保存</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import request from "@/utils/request";
import {ElMessage} from "element-plus";

export  default {
  name:'Person',
  data(){
    return{
      form:{}
    }
  },
created() {
    let str=sessionStorage.getItem("user")||"{}"
    this.form=JSON.parse(str)
},
  methods:{
    update(){
      request.put("/user/update",this.form).then(res =>{
        console.log(res)
        if(res.code==='0'){
          ElMessage({
            type:'success',
            message:'更新成功',
          })
          //获得在sessionStorange存储的信息
          sessionStorage.setItem("user",JSON.stringify(this.form))
          //触发Layout更新用户信息
          this.$emit("userInfo")
        }else{
          ElMessage({
            type:"error",
            message:res.msg,
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>